<?php

namespace app\adminmall\controller;
use app\web\model\GoodsDiscountModel;
use app\web\model\GoodsGrabModel;
use app\web\model\GoodPintuansModel;
use app\web\model\GoodsScoreModel;
use app\web\model\GoodsTypeModel;
use app\web\model\StoreGoodsTypeModel;
use app\web\model\CategoryModel;
use app\web\model\SpecModel;
use app\web\model\SpecValueModel;
use app\web\model\ScoreModel;
use app\web\model\AgentModel;
use app\adminmall\model\BrandModel;
use app\common\controller\AdminBaseController;
use think\Db;
/**
 *商品
 */
class GoodsController extends AdminBaseController
{
	protected $discount_model;
	protected $grab_model;
	protected $pintuan_model;
	protected $goodsScore_model;
	protected $mall_category_model;
	protected $goodsType_model;
	protected $spec_model;
	protected $score_model;
	protected $agent_model;
	protected $storeGoodsType_model;
	protected $brand_model;
	 
	 
	
    protected function _initialize() {
        parent::_initialize();
		
		$this->agent_model = new AgentModel();
		
		//商品4栏目
        $this->discount_model = new GoodsDiscountModel();
		$this->grab_model = new GoodsGrabModel();
		$this->pintuan_model = new GoodPintuansModel();
		$this->goodsScore_model = new GoodsScoreModel();
		$this->spec_model = new SpecModel();
		$this->specValue_model = new SpecValueModel();
		$this->brand_model = new BrandModel();
		
		//商品分类
		$this->goodsType_model = new GoodsTypeModel();
		$this->storeGoodsType_model = new StoreGoodsTypeModel();
		
		$goodsType_list = $this->goodsType_model->select();
		$goodsType_list=array2level($goodsType_list);
		
		$storeGoodsType_list = $this->storeGoodsType_model->select();
		$storeGoodsType_list=array2level($storeGoodsType_list);
		
		
		//店铺规格
		$spec_list = $this->spec_model->where('agent_id',session('storeInfo')['id'])->select();
		
		
		$this->assign('goodsType_list',$goodsType_list);
		$this->assign('storeGoodsType_list',$storeGoodsType_list);
		$this->assign('spec_list',$spec_list);
		$this->assign('score',session('storeInfo')['score']);
		//当前店铺信息
		
		
		$brand_list = $this->brand_model->select();
	
		$this->assign('brand_list',$brand_list);
		
    }
	
	
	
    /**
     * 商品列表
	 * @param int $page
     */
    public function index($page = 1,$type){
		$type=request()->param('type');
		$goods_state = request()->param('goods_state');//状态
		$type_model = $type.'_model';
		$field = 'g.*,a.store_name';
		$list = Db::name("mall_goods_".$type)->alias('g')->join('mall_agent a ','g.agent_id = a.id')->field($field)->where('goods_state',$goods_state)->paginate(10);
		$map = [];
    	if(request()->isPost()){
		 	$param = input('post.');
		 	//商品名称
		$map['goods_state']=$goods_state ;
			if($param['slt_goods'] ){
				$map['g.name'] = ['like', '%'.$param['slt_goods'].'%'];
			}
			//商户名称
			if($param['store_name']){
				$map['store_name'] = ['like', '%'.$param['store_name'].'%'];
			} 
			//审核状态
			if($param['examine_state']!=2 ){
				$map['examine_state'] = ['like', '%'.$param['examine_state'].'%'];
			}
			// 添加时间
			if($param['add_time']&&$param['end_time']){
             $map['g.add_time'] = ['between time', [$param['add_time'],$param['end_time']]];

			}
			$list = Db::name("mall_goods_".$type)->alias('g')->join('mall_agent a ','g.agent_id = a.id')->field($field)->where($map)->paginate(10);
	 }
		
		return $this->fetch('index', ['list' => $list,'add_type'=>'add'.$type,'type'=>$type,'goods_state'=>$goods_state]);
    }


    /**
     *添加疯狂抢商品
     */
    public function addGrab($pid = '')
    {
        if(request()->isAjax()){
        	$param = input('post.');
			$param['add_time'] = now_time();
			$param['agent_id'] = session('storeInfo')['id'];
			//$param['m_id'] = session('storeInfo')['pid'];
			
			//商品规格
			//$param['goods_spec'] = json_encode(array('spec_id'=>$param['goods_spec'],'specval'=>$param['specval_item'] ),JSON_UNESCAPED_UNICODE);
			
			
			if ($this->grab_model->allowField(true)->save($param) !== false) {
                    $this->success('添加成功', url('Goods/index',['type'=>'grab']));
            }else{
                $this->error('添加失败');
            }
        }
		
		 return $this->fetch();
    }
	
	/**
     *添加优惠购商品
     */
    public function addDiscount($pid = '')
    {
        if(request()->isAjax()){
        	$param = input('post.');
			$param['add_time'] = now_time();
			$param['agent_id'] = session('storeInfo')['id'];
			//$param['m_id'] = session('storeInfo')['pid'];
			
			if ($this->discount_model->allowField(true)->save($param) !== false) {
                    $this->success('添加成功', url('Goods/index',['type'=>'grab']));
            }else{
                $this->error('添加失败');
            }
        }
		
		 return $this->fetch();
    }
	
	
	
	/**
     *添加赚积分商品
     */
    public function addGoodsScore($pid = '')
    {
        if(request()->isAjax()){
        	$param = input('post.');
			$param['add_time'] = now_time();
			$param['agent_id'] = session('storeInfo')['id'];
			//$param['m_id'] = session('storeInfo')['pid'];
			
			if ($this->goodsScore_model->allowField(true)->save($param) !== false) {
                    $this->success('添加成功', url('Goods/index',['type'=>'grab']));
            }else{
                $this->error('添加失败');
            }
        }
		 return $this->fetch();
    }
	
	/**
     *出售中的产品
     */
    public function sellGoods($page = 1)
    {
		$type=request()->param('type');
		
		$type_model = $type.'_model';
		
    	$list = $this->$type_model->where('goods_state',2)->paginate(10);
		
		 if(request()->isPost()){
		 	$param = input('post.');
			$slt_goods = '%'.$param['slt_goods'].'%';
			$list = $this->$type_model->where('name', 'like', $slt_goods)->where('goods_state',2)->paginate(10);
		 }
		return $this->fetch('index', ['list' => $list,'add_type'=>'add'.$type,'type'=>$type]);
    }
	
	
	/**
     *已售罄商品
     */
    public function sellout ($pid = '')
    {
       $type=request()->param('type');
		$type_model = $type.'_model';
    	$list = $this->$type_model->where('goods_state',3)->paginate(10);
		
		 if(request()->isPost()){
		 	$param = input('post.');
			$slt_goods = '%'.$param['slt_goods'].'%';
			$list = $this->$type_model->where('name', 'like', $slt_goods)->where('goods_state',3)->paginate(10);
		 }
		return $this->fetch('index', ['list' => $list,'add_type'=>'add'.$type,'type'=>$type]);
    }
	
	
    /**
     * [offshelf 上下架]
     * @return 
     */
    public function offshelf($id,$type,$goods_state)
    {
    	$param = input('post.');     
		$type = input('param.type');		
		$type_model = $type.'_model';
		$state = '2';
		//出售中、已售罄商品下架操作    1、下架  2、上架
		if($goods_state == '2' || $goods_state == '3'){
			$state = '1';
		}
     	if ($this->$type_model->allowField(true)->save(['goods_state'=>$state],['id'=>$id]) !== false) {
            $this->success('修改成功', url('Goods/index',['type'=>$type,'goods_state'=>$goods_state]));
        } else {
            $this->error('修改失败');
        }
        $id = input('param.id');
		$this->assign('pid',null);
		$this->assign('type',$type);
		$list = $this->$type_model->where('goods_state',$state)->paginate(10);
        return $this->fetch('index', ['list' => $list]);
    }
	
	
	/**
     *回收站
     */
    public function deleted($pid = '')
    {
        $type=request()->param('type');
		
		$type_model = $type.'_model';
		
    	$list = $this->$type_model->where('goods_state',4)->paginate(10);
		
		 if(request()->isPost()){
		 	$param = input('post.');
			$slt_goods = '%'.$param['slt_goods'].'%';
			$list = $this->$type_model->where('name', 'like', $slt_goods)->where('goods_state',4)->paginate(10);
		 }
		return $this->fetch('index', ['list' => $list,'add_type'=>'add'.$type,'type'=>$type]);
    }
	


    /**
     * [edit 编辑]
     * @return 
     */
    public function edit($id,$type)
    {
    	 $type=request()->param('type');
		
		$type_model = $type.'_model';
		 $id = input('param.id');
        $this->assign('ad',$this->discount_model->find($id));
		$this->assign('pid',null);
		$this->assign('type',$type);
		
        if(request()->isAjax()){
            $param = input('post.');  
                
         	if ($this->discount_model->allowField(true)->save($param,$id) !== false) {
                    $this->success('修改成功', url('Goods/index',['type' => $type]));
            } else {
                $this->error('修改失败');
            }
        }
       
		
        return $this->fetch();
    }
	/**
     * [examine 审核]
     * @return 
     */
    public function examine($id,$examine_state,$goods_state)
    {	   
		$type = input('param.type');
		$type_model = $type.'_model';		
        if(request()->isAjax()){
        	$param = input('post.');  
         	if ($this->$type_model->allowField(true)->save(['examine_state'=>'1'],['id'=>$id]) !== false) {
                    $this->success('操作成功', url('Goods/index',['type'=>$type,'goods_state'=>$goods_state]));
            } else {
                $this->error('操作失败');
            }
        }
        $id = input('param.id');
		$this->assign('type',$type);
		$this->assign('examine_state',$examine_state);
		$this->assign('goods_state',$goods_state);
		
        return $this->fetch();
    }
	
	



    /**
     * [del删除]
     */
    public function delete()
    {
        $id = input('param.id');
		$type = input('param.type');
		
		$type_model = $type.'_model';
		
        if ($this->$type->destroy($id)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
	
	 /**
     * [商品类型]
	  * @param int
     */
    public function storegoodstype($pid)
    {
       if(request()->isAjax()){
		   if(intval($pid)){
		   		$child =$this->storeGoodsType_model->where('pid',$pid)->select();
			   return $child;
		   }else{
		   		return 1;
		   }
        }
        
    }
	
	 /**
     * [商品规格值]
	  * @param int
     */
    public function specval($spec_id)
    {
       if(request()->isAjax()){
            $param = input('post.'); 
		   if(intval($spec_id)){
		   		$child =$this->specValue_model->where('spec_id',$spec_id)->select();
			   
			   return $child;
		   }
        }
    }
     /**
     * 查询
	  * @param int
     */
    public function seach($type,$goods_state)
    {
        $map = [];
        $type_model = $type.'_model';
        
		$goods_state = request()->param('goods_state');
		$field = 'g.*,a.store_name';

		if(request()->isPost()){
		 	$param = input('post.');
		 	//商品名称

			if($param['slt_goods'] ){
				$map['g.name'] = ['like', '%'.$param['slt_goods'].'%'];
			}
			//商户名称
			if($param['store_name']){
				$map['store_name'] = ['like', '%'.$param['store_name'].'%'];
			}
	 }
		$list = Db::name("mall_goods_grab")->alias('g')
		->join('mall_agent a ','g.agent_id = a.id')->field($field)->where($map)->paginate(10);

        return $this->fetch('index', ['list' => $list,'type'=>$type,'goods_state'=>$goods_state]);
    }
}